package com.atguigu.jxc.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {

    private Integer code;
    private String msg;
    private Map<String, Object> info = new HashMap<>();

    /**
     * 构造函数私有化
     */
    private R(){}

    /**
     * 返回成功结果
     * @return
     */
    public static R ok(){
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMsg(ResponseEnum.SUCCESS.getMessage());
        //230911 接口文档上的要求是info为null, 实际上我看前端页面接收到的数据为{}应该也是null的意思吧,也可以手的的设置为null测试一下
        //修改之前的响应: {"code":0,"msg":"成功","info":{}} 修改之后的响应{"code":0,"msg":"成功","info":null}
        r.setInfo(null);
        return r;
    }

    /**
     * 返回失败结果
     * @return
     */
    public static R error(){
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMsg(ResponseEnum.ERROR.getMessage());
        return r;
    }

    /**
     * 设置特定的结果
     * @param responseEnum
     * @return
     */
    public static R setResult(ResponseEnum responseEnum){
        R r = new R();
        r.setCode(responseEnum.getCode());
        r.setMsg(responseEnum.getMessage());
        return r;
    }

    public R data(String key, Object value){
        this.info.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setInfo(map);
        return this;
    }

    /**
     * 设置特定的响应消息
     * @param message
     * @return
     */
    public R message(String message){
        this.setMsg(message);
        return this;
    }


    /**
     * 设置特定的响应码
     * @param code
     * @return
     */
    public R code(Integer code){
        this.setCode(code);
        return this;
    }
}
